from django.http import Http404
from django.shortcuts import render

# Create your views here.
from rest_framework.generics import GenericAPIView, ListAPIView, RetrieveAPIView
from rest_framework.mixins import ListModelMixin
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework_extensions.cache.decorators import cache_response

from areas.models import Area
from areas.serializers import AreaSerializer, SubAreaSerializer


# GET /areas/
class AreasView(ListAPIView):
    serializer_class = AreaSerializer

    # 指定当前视图所使用的查询集
    queryset = Area.objects.filter(parent=None)

    # def get(self, request):
    #     """
    #     获取所有省级地区信息
    #     1.查询获取所有省级地区的信息
    #     2.将省级地区的信息序列化并返回
    #     """
    #     # 1.查询获取所有省级地区的信息
    #     areas = self.get_queryset()
    #
    #     # 2.将省级地区的信息序列化并返回
    #     serializer = self.get_serializer(areas, many=True)
    #
    #     return Response(serializer.data)


# GET/areas/(?P<pk>\d+)/
class SubAreasView(RetrieveAPIView):
    serializer_class = SubAreaSerializer

    queryset = Area.objects.all()

    # get_object(): 从查询集获取指定的对象,默认根据pk进行查询
    # def get(self, request, pk):
    #     """
    #     获取指定地区的信息
    #     1. 根据pk获取指定地区的信息
    #     2. 将指定地区的信息序列化并返回(下级进行嵌套序列化)
    #     """
    #     # 根据pk获取指定地区的信息
    #     # try:
    #     #     area = Area.objects.get(pk=pk)
    #     # except Area.DoesNotExist:
    #     #     raise Http404
    #
    #     area = self.get_object()
    #
    #     # 将指定地区的信息序列化并返回(下级地区进行嵌套序列化)
    #     serializer = self.get_serializer(area)
    #     return Response(serializer.data)
